import Axios from 'axios';
import qs from 'qs';
import baseUrl from './index';
import cookieUse from '@/libs/cookie.js';
let baseURL;
if (process.env.NODE_ENV === 'development') {
    baseURL = baseUrl;
    // dev
    // baseURL = 'http://172.172.10.101:18769';
} else {
    // build
    baseURL = baseUrl;
}
const instance = Axios.create({
    baseURL: baseURL,
    timeout: 0, // 导出不设定超时时间
    responseType: 'blob'
});

// 请求拦截器
instance.interceptors.request.use(
    (config) => {
        let token = cookieUse.getCookie('desig-token') || '';
        config.headers.Authorization = token;
        return config;
    },
    (err) => Promise.reject(err)
);

// 导出文件 params=>{id或path}
export const downloadFile = (params) => {
    let url = `/minio/downloadByte`;
    return instance.get(url, {
        params,
        headers: {
            Accept: 'application/octet-stream'
        }
    });
};
export const downLoadMdFile = (path) => {
    return instance({
        url: `/minio/downloadByte`,
        //url: `${baseUrl}/minio/download/MINIO-7e-4395-4c72-a637-b586fcd2604e`,
        //url: `../static/README.md`,
        params: path,
        method: 'get',
        headers: {
            Accept: 'application/octet-stream'
        }
    });
};
// 导出导入客观题模板
export const downloadObjectiveQuestionTemplate = () => {
    let url = `/new-paper/objective/question/excel-template`;
    return instance.get(url);
};

export const downloadClassSeat = (classId) => {
    let url = `/self-study/box/environment/exportClassRoomSeat/${classId}`;
    return instance.get(url, {
        headers: {
            'Content-Type': 'application/octet-stream'
        }
    });
};

// 培训课节报告
export const downloadClassReporter = (classNumberId) => {
    let url = `/self-study/trainStatistics/download/${classNumberId}`;
    return instance.post(
        url,
        {},
        {
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/octet-stream'
            }
        }
    );
};

// ctf环境信息
export const downloadCtfVmInfo = (id) => {
    let url = `/compete/env/exportCompeteTeamSeatAndEnVm/${id}`;
    return instance.get(
        url,
        {},
        {
            headers: {
                Accept: 'application/octet-stream'
            }
        }
    );
};

// ctf团队排行
export const downloadCtfRankReporter = (params) => {
    let url = `/compete/ctfGameBox/export/${params.id}/${params.name}`;
    return instance.get(url, {
        headers: {
            Accept: 'application/octet-stream'
        }
    });
};

// ctf积分排行
export const downloadCtfScoreReporter = (params) => {
    let url = `/compete/ctfGameBox/exportSubmit/${params.id}/${params.name}`;
    return instance.get(url, {
        headers: {
            Accept: 'application/octet-stream'
        }
    });
};

// ctf答题报告
export const downloadCtfAnswerReporter = (id, data) => {
    let url = `/compete/ctfReport/${id}/downloadZip`;
    return instance.post(url, data, {
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/octet-stream'
        }
    });
};

export const downloadCtfGroupReporter = (id, gid, data) => {
    let url = `/compete/ctfReport/download/${id}/${gid}`;
    return instance.post(url, data, {
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/octet-stream'
        }
    });
};

export const downloadCtfSeat = (id) => {
    let url = `/self-study/box/environment/exportCompeteTeamSeat/${id}`;
    return instance.get(url, {
        headers: {
            'Content-Type': 'application/octet-stream'
        }
    });
};

export const downloadGuidebook = (id) => {
    let url = `/self-study/guide/expGuide/exportExpGuideWord/${id}`;
    return instance.get(url, {
        headers: {
            Accept: 'application/octet-stream'
        }
    });
};

// 导出学员xlsx
export const downloadStudent = (params) => {
    let url = `/identity/company/users/export`;
    return instance.get(url, {
        params,
        paramsSerializer: (params) => {
            return qs.stringify(params, { indices: false });
        },
        headers: {
            'Content-Type': 'application/octet-stream'
        }
    });
};

// 导出客观题xlsx
export const downloadObjectiveQuestion = (data) => {
    let url = `/new-paper/objective/question/export`;
    return instance.post(url, data, {
        headers: {
            'Content-Type': 'application/json',
            Accept: '*/*'
        }
    });
};

//导出实验组
export const exportGroups = (params) => {
    return instance({
        url: `/identity/experimenterGroups/export`,
        method: 'get',
        params: params,
        paramsSerializer: (params) => {
            return qs.stringify(params, { indices: false });
        },
        headers: {
            'Content-Type': 'application/json; application/octet-stream'
        },
        responseType: 'blob'
    });
};

//导出归档文件
export const downloadFilingFile = (id) => {
    return instance({
        url: `/compete/record/downloadZip/${id}`,
        method: 'get',
        params: {},
        headers: {
            'Content-Type': 'application/json; application/octet-stream'
        },
        responseType: 'blob'
    });
};

//打包导出所有报告
export const downloadSceneAllReport = (sceneId, ids, fn) => {
    return instance({
        url: `/compete/ctfReport/${sceneId}/downloadZip`,
        method: 'post',
        data: ids,
        headers: {
            'Content-Type': 'application/json; application/octet-stream'
        },
        responseType: 'blob',
        onDownloadProgress: fn
    });
};

//夺旗赛提交统计导出报告
export const downloadSubmitAllReport = (sceneId, teamName, ids) => {
    return instance({
        url: `/compete/ctfReport/download/${sceneId}/${teamName}`,
        method: 'post',
        data: ids,
        headers: {
            'Content-Type': 'application/json; application/octet-stream'
        },
        responseType: 'blob'
    });
};

/**
 * 打包导出指定护网演练的所有报告
 * @param {*} id
 * @returns
 */
export const downloadReports = (id, reportIds, fn) => {
    return instance({
        url: `/referee/referee/downloadZip/${id}`,
        method: 'post',
        data: reportIds,
        headers: {
            'Content-Type': 'application/json; application/octet-stream'
        },
        responseType: 'blob',
        onDownloadProgress: fn
    });
};

// git项目下载
export const gitDownload = (projectId, params, fn) => {
    return instance({
        url: `/scientific/gitManage/download/${projectId}`,
        method: 'GET',
        params: params,
        data: {},
        headers: {
            'Content-Type': 'application/json; application/octet-stream',
        },
        responseType: 'blob',
        onDownloadProgress: fn,
    });
};